package com.ruoyi.project.manage.project.controller;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.service.IApproveLogService;
import com.ruoyi.project.manage.detail.domain.ProjectInvestDetail;
import com.ruoyi.project.manage.detail.service.IProjectInvestDetailService;
import com.ruoyi.project.manage.invest.service.IProjectInvestService;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.service.IProjectService;
import com.ruoyi.project.manage.sixDone.service.IProjectSixDoneService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目审核Controller
 *
 * @author FK 2022/3/30
 * @since v1.0.0
 **/
@Controller
@RequestMapping("/manage/approval")
public class ApprovalProjectController extends BaseController {
    private String prefix = "manage/approval";

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IApproveLogService approveLogService;
    @Autowired
    private IProjectInvestService projectInvestService;
    @Autowired
    private IProjectSixDoneService projectSixDoneService;
    @Autowired
    private IProjectInvestDetailService investDetailService;

    @RequiresPermissions("manage:approval:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    @RequiresPermissions("manage:approval:view")
    @GetMapping("/invest")
    public String projectInvest()
    {
        return prefix + "/projectInvest";
    }

    @RequiresPermissions("manage:approval:view")
    @GetMapping("/sixDone")
    public String projectSixDone()
    {
        return prefix + "/projectSixDone";
    }

    /**
     * 查询项目审核列表
     */
    @RequiresPermissions("manage:approval:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        project.setProjectStatusStr("20,30,40");
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 查询资金审核列表
     */
    @RequiresPermissions("manage:approval:list")
    @PostMapping("/invest/list")
    @ResponseBody
    public TableDataInfo investList(Project project)
    {
        startPage();
        project.setInvestStatusStr("20,30,40");
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 查询六定审核列表
     */
    @RequiresPermissions("manage:approval:list")
    @PostMapping("/sixDone/list")
    @ResponseBody
    public TableDataInfo sixDoneList(Project project)
    {
        startPage();
        project.setSixDoneStatusStr("20,30,40");
        project.setIsSixDone(ProjectConstants.YES);
        project.setHasInvestDone(ProjectConstants.YES + "");
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 审核项目信息
     */
    @RequiresPermissions("manage:approval:view")
    @GetMapping("/detail/{projectId}")
    public String detail(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);

        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_PROJECT);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/projectDetail";
    }

    /**
     * 审核资金信息
     */
    @RequiresPermissions("manage:approval:view")
    @GetMapping("/invest/detail/{projectId}")
    public String investDetail(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);
        // 获取当前年
        String currentYear = DateUtils.dateTimeNow("yyyy");
        mmap.put("currentYear", currentYear);
        // 项目投资情况统计
        List<Map> investStat = investDetailService.statProjectInvest(projectId);
        if (investStat.size() == 0) {
            Map defaultMap = new HashMap<>();
            defaultMap.put("year", currentYear);
            defaultMap.put("doneInvest", 0);
            investStat.add(defaultMap);
        }
        mmap.put("investStat", investStat);
        // 项目投资明细
        ProjectInvestDetail detailParam = new ProjectInvestDetail();
        detailParam.setProjectId(projectId);
        detailParam.setYear(Integer.valueOf(currentYear));
        List<ProjectInvestDetail> projectInvestDetails = investDetailService.selectProjectInvestDetailList(detailParam);

        if (projectInvestDetails == null || projectInvestDetails.size() == 0) {
            projectInvestDetails = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                ProjectInvestDetail detail = new ProjectInvestDetail();
                detail.setYear(Integer.valueOf(currentYear));
                detail.setMonth(i);
                detail.setProjectId(projectId);
                projectInvestDetails.add(detail);
            }
        }

        mmap.put("projectInvestDetails", projectInvestDetails);
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_INVEST);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/projectInvestDetail";
    }

    /**
     * 审核六定信息
     */
    @RequiresPermissions("manage:approval:view")
    @GetMapping("/sixDone/detail/{projectId}")
    public String sixDoneDetail(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);
        // 获取当前年
        String currentYear = DateUtils.dateTimeNow("yyyy");
        mmap.put("currentYear", currentYear);
        // 项目投资情况统计
        List<Map> investStat = investDetailService.statProjectInvest(projectId);
        if (investStat.size() == 0) {
            Map defaultMap = new HashMap<>();
            defaultMap.put("year", currentYear);
            defaultMap.put("doneInvest", 0);
            investStat.add(defaultMap);
        }
        mmap.put("investStat", investStat);
        // 项目投资明细
        ProjectInvestDetail detailParam = new ProjectInvestDetail();
        detailParam.setProjectId(projectId);
        detailParam.setYear(Integer.valueOf(currentYear));
        List<ProjectInvestDetail> projectInvestDetails = investDetailService.selectProjectInvestDetailList(detailParam);

        if (projectInvestDetails == null || projectInvestDetails.size() == 0) {
            projectInvestDetails = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                ProjectInvestDetail detail = new ProjectInvestDetail();
                detail.setYear(Integer.valueOf(currentYear));
                detail.setMonth(i);
                detail.setProjectId(projectId);
                projectInvestDetails.add(detail);
            }
        }

        mmap.put("projectInvestDetails", projectInvestDetails);
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_SIXDONE);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/projectSixDoneDetail";
    }

    /**
     * 入库项目
     */
    @RequiresPermissions("manage:approval:agree")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/agree")
    @ResponseBody
    public AjaxResult agree(int projectId)
    {
        return toAjax(projectService.agree(projectId));
    }

    /**
     * 拒绝项目
     */
    @RequiresPermissions("manage:approval:reject")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult editSave(Project project)
    {
        return toAjax(projectService.reject(project));
    }

    /**
     * 入库项目资金
     */
    @RequiresPermissions("manage:approval:agree")
    @Log(title = "资金审批入库", businessType = BusinessType.UPDATE)
    @PostMapping("/invest/agree")
    @ResponseBody
    public AjaxResult investAgree(int projectId)
    {
        return toAjax(projectInvestService.agree(projectId));
    }

    /**
     * 拒绝项目资金
     */
    @RequiresPermissions("manage:approval:reject")
    @Log(title = "资金审批驳回", businessType = BusinessType.UPDATE)
    @PostMapping("/invest/reject")
    @ResponseBody
    public AjaxResult investReject(Project project)
    {
        return toAjax(projectInvestService.reject(project));
    }

    /**
     * 入库项目六定
     */
    @RequiresPermissions("manage:approval:agree")
    @Log(title = "六定审批入库", businessType = BusinessType.UPDATE)
    @PostMapping("/sixDone/agree")
    @ResponseBody
    public AjaxResult sixDoneAgree(int projectId)
    {
        return toAjax(projectSixDoneService.agree(projectId));
    }

    /**
     * 拒绝项目六定
     */
    @RequiresPermissions("manage:approval:reject")
    @Log(title = "六定审批驳回", businessType = BusinessType.UPDATE)
    @PostMapping("/sixDone/reject")
    @ResponseBody
    public AjaxResult sixDoneReject(Project project)
    {
        return toAjax(projectSixDoneService.reject(project));
    }
}
